package com.share.example.stock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.common.utils.BeanUtil;
import com.share.example.stock.mapper.UserMapper;
import com.share.example.stock.model.db.StockBaseInfo;
import com.share.example.stock.model.db.User;
import com.share.example.stock.model.rsp.CollectReq;
import com.share.example.stock.model.rsp.CollectRsq;
import com.share.example.stock.service.IStockBaseInfoService;
import com.share.example.stock.service.IStockDayPriceService;
import com.share.example.stock.service.IStockMinPriceService;
import com.share.example.stock.service.IUserService;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-11-16
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Resource
    private UserMapper userMapper;
    @Autowired
    private IStockBaseInfoService stockBaseInfoService;
    @Autowired
    private IStockDayPriceService stockDayPriceService;
    @Autowired
    private IStockMinPriceService stockMinPriceService;

    @Override
    public List<CollectRsq> collectList(CollectReq req) {
        List<CollectRsq> reList = new LinkedList<>();
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(req.getCode())) {
            queryWrapper.like(User::getCollectCodes, req.getCode());
        }
        if (StringUtils.isNotBlank(req.getUsername())) {
            queryWrapper.like(User::getUsername, req.getUsername());
        }
        List<User> usersList = userMapper.selectList(queryWrapper);
        for (User user : usersList) {
            String collectCodes = user.getCollectCodes();
            if (StringUtils.isBlank(collectCodes)) {
                continue;
            }

            String[] split = collectCodes.split(",");
            if (ArrayUtils.isEmpty(split)) {
                continue;
            }

            for (String code : split) {
                if (StringUtils.isBlank(code)) {
                    continue;
                } else {
                    if (StringUtils.isNotBlank(req.getCode())) {
                        //有条件赛选
                        if (!code.contains(req.getCode())) {
                            continue;
                        }
                    }
                }

                //结果集展示
                StockBaseInfo stockCodeMap = stockBaseInfoService.getStockCodeMap(code);
                if (stockCodeMap != null) {
                    CollectRsq item = new CollectRsq();
                    BeanUtil.copyProperties(item, stockCodeMap);
                    item.setUsername(user.getUsername());
                    item.setEmail(user.getEmail());

                    if ("1".equals(req.getShowGraph())) {
                        item.setDays(stockDayPriceService.listByCode(code));
                        item.setMinutes(stockMinPriceService.listByCode(code));
                    }
                    reList.add(item);
                }
            }
        }

        return reList;
    }

    @Override
    public Set<String> listAllCollect() {
        Set<String> reSet = new HashSet<>();

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getStatus, 1);
        List<User> list = userMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(list)) {
            for (User item : list) {
                if (StringUtils.isBlank(item.getStrategys())) {
                    //没有配置,跳过
                    continue;
                }

                String[] split = item.getStrategys().split(",");
                if (ArrayUtils.isNotEmpty(split)) {
                    Arrays.stream(split).forEach(e -> {
                        if (StringUtils.isNotBlank(e) && e.length() > 4) {
                            reSet.add(e);
                        }
                    });
                }
            }
        }
        return reSet;
    }

    @Override
    public List<String> listEmailByStrategy(String strategy) {
        List<String> relist = new LinkedList<>();
        if (StringUtils.isBlank(strategy)) {
            return relist;
        }

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getStatus, 1);
        List<User> list = userMapper.selectList(queryWrapper);
        for (User item : list) {
            if (StringUtils.isBlank(item.getStrategys())) {
                //没有配置,跳过
                continue;
            }

            if (StringUtils.isBlank(item.getEmail())) {
                continue;
            }

            //匹配是否填写
            if (item.getStrategys().contains("all")) {
                relist.add(item.getEmail());
            } else if (item.getStrategys().contains(strategy)) {
                relist.add(item.getEmail());
            }
        }
        return relist;
    }
}
